package org.tacademy.remoteclient;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MyRemoteClientActivity extends Activity {
    /** Called when the activity is first created. */
    
	org.tacademy.remoteservice.ICalc calc;
	TextView resultText;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        resultText = (TextView)findViewById(R.id.result);
        
        Button addBtn = (Button)findViewById(R.id.addButton);
        addBtn.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				int result = 0;
				
				try {
					result = calc.getAdd(5,10);
				} catch (RemoteException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				resultText.setText("add result="+result);
				
			}
		});
        
        Button primeBtn = (Button)findViewById(R.id.primeButton);
        primeBtn.setOnClickListener(new OnClickListener() {
        	boolean isPrime = false;
			public void onClick(View v) {
				try {
					isPrime = calc.isPrime(7);
				} catch (RemoteException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				resultText.setText("7 is prime Number: "+ isPrime);
			}
		});
    }
	
	ServiceConnection srvconn = new ServiceConnection() {
		
		public void onServiceDisconnected(ComponentName name) {
			
		}
		
		public void onServiceConnected(ComponentName name, IBinder service) {
			calc = org.tacademy.remoteservice.ICalc.Stub.asInterface(service);
		}
	};
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		unbindService(srvconn);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		Intent intent = new Intent("org.tacademy.remoteservice.CALC");
		bindService(intent,srvconn,BIND_AUTO_CREATE);
	}
}















